package controller;

import entity.hotel_order;
import entity.hotel_room;
import entity.hotel_tenant;
import service.HotelEmployeeService;
import service.HotelOrderService;
import serviceImp.HotelEmployeeServiceImp;
import serviceImp.HotelOrderServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Servlet implementation class RegisterController
 */
@WebServlet("/OrderCreateController.do")
public class OrderCreateController extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderCreateController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String tenant_name = request.getParameter("tenant_name");
		String tenant_sex = request.getParameter("tenant_sex");
		String tenant_id = request.getParameter("tenant_id");
		String tenant_tel = request.getParameter("tenant_tel");
		String room_class = request.getParameter("room_class");
		String order_remarks = request.getParameter("order_remarks");

		HttpSession session = request.getSession();
		String employee_tel = (String)session.getAttribute("loginTel");

		Date date =new Date();
		HotelOrderService hotelOrderService = new HotelOrderServiceImp();

		List<hotel_room> roomList = hotelOrderService.selectRoomByClass(room_class);

		if(roomList==null){
			request.setAttribute("orderTip","该类型房间已满，请重新选择");
			request.getRequestDispatcher("/hotelSystem/order/createOrder.jsp").forward(request,response);
		}else {
			hotel_room hotel_room = roomList.get(0);
			HotelEmployeeService hotelEmployeeService = new HotelEmployeeServiceImp();

			String employee_id = hotelEmployeeService.selectIdByTel(employee_tel);

			hotel_tenant hotel_tenant = new hotel_tenant(tenant_name,tenant_sex,tenant_id,tenant_tel,hotel_room.getRoom_no());
			hotel_order hotel_order = new hotel_order(null,hotel_room.getRoom_no(),hotel_room.getRoom_price()
					,employee_id,date,order_remarks);

			int tenantCode = hotelOrderService.tenantCreate(hotel_tenant);
			if(tenantCode==0){
				request.setAttribute("failTip","房客信息录入失败，请重试");
				request.getRequestDispatcher("/hotelSystem/order/createOrder.jsp").forward(request,response);
			}

			int orderCode = hotelOrderService.orderCreate(hotel_order);
			if(orderCode==0){
				request.setAttribute("failTip","订单信息录入失败，请重试");
				request.getRequestDispatcher("/hotelSystem/order/createOrder.jsp").forward(request,response);
			}

			hotelOrderService.roomStatusChange(hotel_tenant.getRoom_no(),"1");
			request.setAttribute("CheckTip","入住信息登记成功");
			request.getRequestDispatcher("/order/orderInfo.jsp").forward(request,response);
		}


	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
